package nckd.wnqjt.tmc.tda.plugin.form;

import kd.bos.algo.DataSet;
import kd.bos.algo.Row;
import kd.bos.entity.report.AbstractReportListDataPlugin;
import kd.bos.entity.report.ReportQueryParam;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhujintao
 * @date 2024/11/05
 * @desc 低效账户单位分布
 */
public class InefficientAccountDetailListDataPlugin extends AbstractReportListDataPlugin {

    @Override
    public DataSet query(ReportQueryParam reportQueryParam, Object o) throws Throwable {
        //筛选出 筛选器传入的ids
        Map<String, Object> customParam = reportQueryParam.getCustomParam();
        List<String> list = (ArrayList<String>) customParam.get("id");
        List<Long> longList = list.stream().map(e -> Long.valueOf(e)).collect(Collectors.toList());
        //先获取到低效账户设置的参数
        //Map<String, Object> settingFilterMap = InefficientAccountQingDataProvider.dormantSettingFilter();
        /*String getDailySelectedFieds = "id,org.id orgid,org.number orgnumber,org.name orgname,accountbank.bankaccountnumber bankaccountnumber,bizdate";
        QFilter dailytimefilter = new QFilter("id", QCP.in, longList);
        //银行日记账
        DataSet bankJournalAllDataSet = QueryServiceHelper.queryDataSet("dormantAcct", "cas_bankjournal", getDailySelectedFieds, dailytimefilter.toArray(), (String) null);
        DataSet accountcountDataSet = bankJournalAllDataSet.groupBy(new String[]{"orgid", "bankaccountnumber"}).count("counttradecount").finish();*/

        //先获取到低效账户设置的参数
        Map<String, Object> settingFilterMap = InefficientAccountQingDataProvider.dormantSettingFilter();
        //先获取符合条件的账户列表
        QFilter acctimefilter = new QFilter("opendate", QCP.less_equals, settingFilterMap.get("queryStartDate"));
        QFilter finorgtypefilter = new QFilter("finorgtype", QCP.not_equals, "1");
        QFilter acctstatusfilter = new QFilter("acctstatus", QCP.not_equals, "closed");
        QFilter[] acctfilters = new QFilter[]{acctimefilter, finorgtypefilter, acctstatusfilter};
        String getAcctSelectedFieds = "id,company.id orgid,openorg,issetbankinterface,bank,bank.bank_cate bankbankcate,bankaccountnumber,name,currencyname,defaultcurrency,opendate,acctstatus,acctstyle,accttype,acctproperty,manager,acctname,finorgtype";
        DataSet acctDataSet = QueryServiceHelper.queryDataSet("dormantAcct", "bd_accountbanks", getAcctSelectedFieds, acctfilters, null);
        List<String> accountNoList = new ArrayList<>();
        DataSet acctDataCopy = acctDataSet.copy();
        while (acctDataCopy.hasNext()) {
            Row next = acctDataCopy.next();
            String bankaccountnumber = next.getString("bankaccountnumber");
            accountNoList.add(bankaccountnumber);
        }
        //银行日记账
        QFilter accountFilter = new QFilter("accountbank.bankaccountnumber", QCP.in, accountNoList);
        String getDailySelectedFieds = "id,org.id orgid,org.number orgnumber,org.simplename orgname,accountbank.bankaccountnumber bankaccountnumber,accountbank.bank.bank_cate bankbankcate,bizdate";
        QFilter dailytimefilter = (new QFilter("bizdate", ">=", settingFilterMap.get("queryStartDate"))).and(new QFilter("bizdate", "<=", settingFilterMap.get("queryEndDate")));
        QFilter[] bankjournalfilters = new QFilter[]{accountFilter, dailytimefilter};
        DataSet bankJournalAllDataSet = QueryServiceHelper.queryDataSet("dormantAcct", "cas_bankjournal", getDailySelectedFieds, bankjournalfilters, null);
        bankJournalAllDataSet = bankJournalAllDataSet.groupBy(new String[]{"bankaccountnumber"}).count("counttradecount").finish();

        DataSet allDataSet = acctDataSet.leftJoin(bankJournalAllDataSet).on("bankaccountnumber","bankaccountnumber")
                .select(new String[]{"orgid","bankaccountnumber","bankbankcate"}, new String[]{"counttradecount"}).finish();

        //拿到明细 获取低于设置的账户
        //获取到的明细数据先根据账户分组 得到账户的低效数
        HashMap<String, Object> count = new HashMap();
        count.put("tradenumber", settingFilterMap.get("tradenumber"));
//        DataSet accountcountDataSet = allDataSet.groupBy(new String[]{"orgid", "orgname", "bankaccountnumber","bankbankcate"}).count("counttradecount").finish();
        //符合条件的账户
        allDataSet = allDataSet.filter("counttradecount < tradenumber", count);

        HashMap<String, Object> countOrgId = new HashMap();
        countOrgId.put("longList", longList);
        allDataSet = allDataSet.filter("orgid in (longList)", countOrgId);
        return allDataSet;
    }
}
